<template>
	<view class="list-common-box">
		<view class="index-content-img-box" v-if="goodsBox.length>0">
			<view class="index-content-img" :class="name==='project'?'index-content-img-fin':''"
				v-for="(item,index) in goodsBox" :key="index" @click="goDetail(item)">
				<image :src="item.image" mode="widthFix" :lazy-load="true"></image>
				<view class="index-content-word" v-if="name!=='project'">
					<view class="index-content-img-title u-ellipsis-1">
						{{item.title}}
					</view>
					<view class="index-content-img-subtitle u-ellipsis-1">
						{{item.subtitle}}
					</view>
					<view class="index-content-img-price" v-if="item.price">
						{{item.price}} <span>{{item.original_price}}</span>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" v-if="goodsBox.length>5"/>
		<view class="list-empty" v-if="goodsBox.length===0">
			<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png">
			</u-empty>
		</view>
	</view>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				list: [],
				name: '',
				goodsObj: {},
				goodsBox: [],
				nameType: '1',
				isloading: false,
				currentPage: 1, //当前分页
				lastPage: 1, //总分页
				total: 0, //总商品数
				perPage: 0, //单页商品数
				status: 'loadmore',
			}
		},
		created() {},
		onLoad() {
			const id = this.$Route.query.id;
			const name = this.$Route.query.name;
			this.name = name;
			this.getData(id)
		},
		// 触底的事件
		onReachBottom() {
			if (this.currentPage > this.lastPage) {
				this.status = 'nomore'
				return uni.$showMsg('数据加载完毕!')
			}
			const id = this.$Route.query.id;
			this.status = 'loading';
			// 让页码值自增+1
			this.currentPage++
			this.getData(id)
		},
		methods: {
			getData(id) {
				uni.showLoading({
					title: '正在加载中。。。',
					mask: true
				})
				this.$http('goods.lists', {
					category_id: id,
					page: this.currentPage,
					per_page:10
				}).then(res => {
					if (res.code === 1) {
						console.log(res.data)
						let list = res.data.data;
						if(list.length===0){
							this.status = 'nomore'
						}
						this.goodsBox = [...this.goodsBox, ...res.data.data];
						console.log(this.goodsBox,'this.goodsBoxthis.goodsBoxthis.goodsBox')
						this.lastPage = res.last_page;
						this.total = res.total;
						this.perPage = res.per_page;
					}
				}).finally(() => {
					uni.hideLoading()
				});
			},
			goDetail(item) {
				let name = this.name;
				if (name === 'project') {
					this.$Router.push({
						path: '/pages/goods/detail',
						query: {
							id: item.id,
							showType: '1'
						}
					})
				} else {
					this.$Router.push({
						path: '/pages/goods/detail',
						query: {
							id: item.id
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.list-empty {
		width: 100%;
		margin: 30% auto 0;
	}

	.list-common-box {
		padding: 30rpx;

		.list-common-img {
			height: 182rpx;
			margin-bottom: 30rpx;
			background: #F6F8FA;
			border-radius: 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			position: relative;

			image {
				width: 100%;
				height: 100%;
				border-radius: 30rpx;
			}

			.list-common-img-word {
				color: #fff;
				font-size: 28rpx;
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
				width: 200rpx;
				height: 100rpx;
				text-align: center;
				line-height: 100rpx;
			}
		}

		.index-content-img-box {
			display: flex;
			justify-content: space-between;
			border-radius: 30rpx;
			flex-wrap: wrap;
		}

		.index-content-img {
			width: 330rpx;
			margin-bottom: 20rpx;

			image {
				width: 100%;
				height: 330rpx;
				border-radius: 30rpx 30rpx 0rpx 0rpx;
				vertical-align: top;
			}
		}

		.index-content-img-fin {
			image {
				border-radius: 30rpx;
			}
		}

		.index-content-word {
			background-color: #fff;
			padding: 20rpx 20rpx 30rpx;
			border-radius: 0rpx 0rpx 30rpx 30rpx;

			.index-content-img-title {
				font-size: 32rpx;
				color: #001226;
				font-weight: 600;
				margin-bottom: 10rpx;
			}

			.index-content-img-subtitle {
				font-size: 24rpx;
				font-weight: 400;
				color: #8E8E8E;
				margin-bottom: 30rpx;
			}

			.index-content-img-price {
				font-size: 28rpx;
				color: #FF0015;
				font-weight: 600;

				span {
					font-size: 24rpx;
					color: #B5B5B5;
					margin-left: 10rpx;
					text-decoration: line-through;
					font-weight: 400;
				}

				span::before {
					content: '￥';
					font-size: 20rpx;
				}
			}

			.index-content-img-price::before {
				content: '￥';
				font-size: 24rpx;
			}
		}
	}

	.productName {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;

		.list-common-img {
			width: 330rpx;
			height: 200rpx;
			border-radius: 30rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}
</style>
